<template>
  <div>
    <el-aside class="left_div">
      <el-row class="tac">
        <el-col :span="24">
          <el-menu
            style="padding-left: 4px"
            default-active="1"
            class="el-menu-vertical-demo"
          >
            <el-menu-item @click="index()"
              ><span class="bt" slot="title">小梁的小破站</span></el-menu-item
            >

            <el-menu-item @click="index()" index="1">
              <i class="el-icon-s-home"></i>
              <span slot="title">首页</span>
            </el-menu-item>
            <el-menu-item>
              <el-input
                type="text"
                prefix-icon="el-icon-search"
                placeholder="搜索..."
              ></el-input>
            </el-menu-item>
            <el-submenu index="2">
              <template slot="title">
                <i class="el-icon-s-order"></i>
                <span>技术文档</span>
              </template>
              <el-menu-item-group>
                <el-menu-item @click="file()" index="2-1">Vue</el-menu-item>
                <el-menu-item index="2-2">Element ui</el-menu-item>
              </el-menu-item-group>

              <el-submenu index="2-3">
                <template slot="title">选项4</template>
                <el-menu-item index="2-3-1">选项4-1</el-menu-item>
              </el-submenu>
              <el-menu-item-group index="2-4">
                <el-menu-item @click="details()">details</el-menu-item>
              </el-menu-item-group>
            </el-submenu>

            <el-menu-item @click="tools()" index="3">
              <i class="el-icon-menu"></i>
              <span slot="title">常用工具</span>
            </el-menu-item>
            <el-menu-item @click="author()" index="4">
              <i class="el-icon-question"></i>
              <span slot="title">关于</span>
            </el-menu-item>

            <el-menu-item @click="drawer = true">
              <i class="el-icon-s-tools"></i>
              <span slot="title">管理</span>
            </el-menu-item>
          </el-menu>
        </el-col>
      </el-row>
    </el-aside>

    <el-drawer title="管理员" :visible.sync="drawer" direction="ltr" size="30%">
      <el-form
        class="p-2"
        style="position: relative; right: 10px"
        ref="searchForm"
        :model="form"
        label-width="80px"
        v-if="showlogin != null"
      >
        <h3>{{ showlogin }}</h3>

        <el-tag @click="adminadd()">添加文章</el-tag>
        <el-tag type="success">添加标签</el-tag>
        <el-tag type="info">标签三</el-tag>
        <el-tag type="warning">标签四</el-tag>
        <el-tag type="danger">标签五</el-tag>
        <el-button @click="exit">退出登录</el-button>
      </el-form>

      <el-form
        class="p-2"
        style="position: relative; right: 10px"
        ref="searchForm"
        :model="form"
        label-width="80px"
        v-else
      >
        <el-form-item label="账号：" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="psw">
          <el-input v-model="form.psw" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="adminlogo()">登录</el-button>
          <el-button @click="resetForm('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "MyHeader",
  inject: ["reload"],
  data() {
    return {
      drawer: false, //左侧弹出
      showlogin: localStorage.getItem("admin"),
      username: {},

      form: {
        name: "",
        psw: "",
      },
    };
  },
  methods: {
    index() {
      if (this.$route.path == "/") {
        return;
      } else {
        this.$router.push("/");
      }
    },
    tools() {
      if (this.$route.path == "/tools") {
        return;
      } else {
        this.$router.push("/tools");
      }
    },
    file() {
      if (this.$route.path == "/file") {
        return;
      } else {
        this.$router.push("/file");
      }
    },
    author() {
      if (this.$route.path == "/author") {
        return;
      } else {
        this.$router.push("/author");
      }
    },
    details() {
      if (this.$route.path == "/details") {
        return;
      } else {
        this.$router.push("/details");
      }
    },
    adminadd() {
      if (this.$route.path == "/adminadd") {
        return;
      } else {
        this.$router.push("/adminadd");
      }
    },
    adminlogo() {
      if (this.form.name == "") {
        this.$message.error("账号不能为空");
      } else if (this.form.psw == "") {
        this.$message.error("密码不能为空");
      } else {
        this.$axios
          .get("http://127.0.0.1/user/adminlogo", {
            params: {
              name: this.form.name,
              psw: this.form.psw,
            },
          })
          .then((res) => {
            if (res.data.status == 200) {
              this.$message({
                message: "登录成功",
                type: "success",
              });
              localStorage.setItem("admin", this.form.name);
              this.reload();
            } else {
              this.$message.error("登录失败，账号或密码错误！");
            }
          })
          .catch((err) => {
            console.log("操作失败" + err);
          });
      }
    },
    resetForm(searchForm) {
      this.$refs[searchForm].resetFields(); //重置表单数据
    },
    exit() {
      // 清除状态保持
      window.localStorage.clear();

      // 状态保持清除后刷新页面
      this.reload();
    },
  },
};
</script>

<style>
@import "../assets/css/all.css";

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 100vh;
}

.el-aside {
  line-height: 100vh;
  display: block;
  position: fixed;
  left: 0;

  bottom: 0;
}
.bt {
  font-weight: bold;
  font-size: 25px;
}

ul li {
  text-align: left;
}
</style>